<?php

require_once ('../Model/getconfig.php');


class Dbo_DbManager {
	protected $dbConnection;
	
	private function getConnection() {
		return $this->dbConnection;
	}
	private function setConnection($dbConnection) {
		$this->dbConnection = $dbConnection;
	}
	public function __construct($bolCon=TRUE) {
		if($bolCon==TRUE){
		$this->getDbConnection ();
		}
	}
	protected function getDbConnection() {
		$arrCon = Dbo_DbDetails::getDbInfo ( "PRODUCTION" );
		$this->dbConnection = new mysqli ( $arrCon ['server'], $arrCon ['username'], $arrCon ['password'], $arrCon ['database'] );
		if ($this->dbConnection->connect_error) {
			die ( 'Connect Error (' . $this->dbConnection->connect_errno . ') ' . $this->dbConnection->connect_error );
		}
	}
	public function clodeDbConnection() {
		if ($this->getConnection () != NULL) {
			$this->setConnection ( "" );
		}
	}
	
	protected function getClassName($objClass) {
		$strClassName = get_class ( $objClass );
		return $strClassName;
	
	}
	
	protected function getTableName($strClassName) {
		$strTableName = "";
		$arrFolderAndClass = explode ( "_", $strClassName );
		$intLen = count ( $arrFolderAndClass );
		$intLen --;
		$strTableName = $arrFolderAndClass [$intLen];
		$strTableName = strtolower ( $strTableName );
		//$strTableName = "tbl" . strtolower ( $strTableName );
		return $strTableName;
	}
	
	protected function getFildsAndKey($strTableName) {
		$arrFields = array ();
		$arrPrimaryKey = array ();
		$arrFieldsAndKey = array ();
		$strQuery = "SHOW FIELDS FROM `$strTableName` ";
		if ($stmtDesc = $this->dbConnection->prepare ( $strQuery )) {
			$stmtDesc->execute ();
			$stmtDesc->bind_result ( $Field, $Type, $Null, $Key, $Default, $Extra );
			$stmtDesc->store_result ();
			while ( $stmtDesc->fetch () ) {
				if ($Key != "PRI") {
					$arrFields [] = $Field;
				} else {
					$arrPrimaryKey [] = $Field;
				}
			}
			
			$arrFieldsAndKey = array ("KEY" => $arrPrimaryKey, "FILEDS" => $arrFields );
		}
		$stmtDesc->close ();
		return $arrFieldsAndKey;
	}
	
	public function checkWorking($objClass) {
		$className = $this->getClassName ( $objClass );
		$tableName = $this->getTableName ( $className );
		$Fields = $this->getFildsAndKey ( $tableName );
	
	}
	public function updateLock() {
		
		if (($remote_addr = $_SERVER ['REMOTE_ADDR']) == '') {
			$remote_addr = "REMOTE_ADDR_UNKNOWN";
		}
		if (($request_uri = $_SERVER ['REQUEST_URI']) == '') {
			$request_uri = "REQUEST_URI_UNKNOWN";
		}
		
		$query = "INSERT INTO tbllock (vchremoteaddr,vchtable) VALUES ('$remote_addr','$tblName')";
		$stmtDesc = $this->dbConnection->prepare ( $query );
		if ($stmtDesc) {
			$stmtDesc->execute ();
		}
	}
	public function write($objClass) {
		$strkey = "";
		$className = $this->getClassName ( $objClass );
		$tableName = $this->getTableName ( $className );
		$arrFieldsAndKey = $this->getFildsAndKey ( $tableName );
		$arrFields = $arrFieldsAndKey ['FILEDS'];
		$arrKey = $arrFieldsAndKey ['KEY'];
		if (count ( $arrKey ) > 0) {
		echo	$strkey = $arrKey ['0'];
		}
		if ($strkey != "") {
			
			/* SQL  INSERT */
			$primKey = "";

			if ($objClass->$strkey == NULL || $objClass->$strkey == "") {

				$arrFieldsToWrite = array ();
				$arrFieldVals = array ();
				$arrParamString = array ();
				foreach ( $arrFields as $field ) {
					
					if ($objClass->$field != NULL && $objClass->$field != "") {
						$arrFieldsToWrite [] = $field;
						$arrFieldVals [] = "'" .mysql_escape_string($objClass->$field). "'";
						$arrParamString [] = "?";
					}
				
				}
				if (count ( $arrFieldsToWrite ) > 0) {
					$strFields = implode ( ', ', $arrFieldsToWrite );
					$strParams = implode ( ',', $arrParamString );
					$strVals = implode ( ', ', $arrFieldVals );
					$query = "INSERT INTO $tableName ($strFields) VALUES ($strVals)";
                    
					$stmtDesc = $this->dbConnection->prepare ( $query );
					$stmtDesc->execute ();
					$stmtDesc->close ();
					$primKey = $this->dbConnection->insert_id;
					$this->updateLock ();
				
				}
			
			} else {
			

				/* SQL  UPDATE */
				
				$arrFieldsToUpdate = array ();
				$arrCondition = array ();
				$arrParamString = array ();
				foreach ( $arrFields as $field ) {

			//	echo $x="haiiii-".$objClass->$field;
					
					if ($objClass->$field != NULL && $objClass->$field != "") {
					
						$arrFieldsToUpdate [] = $field . '=' . "'" . $objClass->$field . "'";
					}
				
				}
				
				if (count ( $arrFieldsToUpdate ) > 0) {
					$strCondition = $strkey . '=' . "'" . $objClass->$strkey . "'";
					$strFieldToUpdate = implode ( ', ', $arrFieldsToUpdate );
					echo $query = "UPDATE  $tableName SET $strFieldToUpdate WHERE $strCondition";
					$stmtDesc = $this->dbConnection->prepare ( $query );
					$stmtDesc->execute ();
					$stmtDesc->close ();
					$this->updateLock ();
					$primKey = $objClass->$strkey;
				
				}
			
			}
			
		//echo $query;
		}
		return $primKey;
	}
	
	public function fetch($arrId = array(), $className) {
		
		$arrResult = array ();
		$tableName = $this->getTableName ( $className );
		$query = "SELECT * FROM $tableName";
		$arrFieldsAndKey = $this->getFildsAndKey ( $tableName );
		$arrKey = $arrFieldsAndKey ['KEY'];
		if (count ( $arrKey ) > 0) {
			$strkey = $arrKey [0];
			if ($arrId ["$strkey"] != NULL) {
				$id = $arrId [$strkey];
				$query = $query . " WHERE $strkey='$id'";
			}
		}
		
		$results_latest = $this->dbConnection->query ( $query );
		while ( $row = $results_latest->fetch_assoc () ) {
			$arrFilter = array ();
			foreach ( $row as $var => $val ) {
				$arrFilter [$var] = htmlentities ( $val );
			}
			$arrResult [] = $arrFilter;
		}
		
		return $arrResult;
	
	}
	
	public function runquery($query) {
		
		$results_latest = $this->dbConnection->query ( $query );
		while ( $row = $results_latest->fetch_assoc () ) {
			$arrFilter = array ();
			foreach ( $row as $var => $val ) {
				$arrFilter [$var] = htmlentities ( $val );
			}
			$arrResult [] = $arrFilter;
		}
		
		return $arrResult;
	
	}
	
	public function remove($id, $className) {
		$tableName = $this->getTableName ( $className );
		$query = "DELETE FROM $tableName ";
		$arrFieldsAndKey = $this->getFildsAndKey ( $tableName );
		$arrKey = $arrFieldsAndKey ['KEY'];
		if (count ( $arrKey ) > 0) {
			$strkey = $arrKey [0];
			if ($id) {
				$query = $query . " WHERE $strkey='$id'";
				$stmtDesc = $this->dbConnection->prepare ( $query );
				$stmtDesc->execute ();
				$stmtDesc->close ();
			}
		}
	}
	
	public function fetchOnCondition($objClass) {
		
		$strkey = "";
		$className = $this->getClassName ( $objClass );
		$tableName = $this->getTableName ( $className );
		
		$arrFieldsAndKey = $this->getFildsAndKey ( $tableName );
		$arrFields = $arrFieldsAndKey ['FILEDS'];
		$arrKey = $arrFieldsAndKey ['KEY'];
		$arrFieldsToSelect = array ();
		if (count ( $arrKey ) > 0) {
			$strkey = $arrKey ['0'];
		}
		
		if ($strkey != "") {
			foreach ( $arrFields as $field ) {
				
				if ($objClass->$field != NULL && $objClass->$field != "") {
					$arrFieldsToSelect [] = $field . '=' . "'" . $objClass->$field . "'";
				}
			
			}
			
			$query = "SELECT * FROM $tableName";
			if (count ( $arrFieldsToSelect ) > 0) {
				$strFieldsToSelect = implode ( ' and ', $arrFieldsToSelect );
				$query = "SELECT * FROM $tableName" . " WHERE " . $strFieldsToSelect;
			
			}
		}

		$results_latest = $this->dbConnection->query ( $query );
		
		while ( $row = $results_latest->fetch_assoc () ) {
			$arrFilter = array ();
			foreach ( $row as $var => $val ) {
				$arrFilter [$var] = htmlentities ( $val );
			}
			$arrResult [] = $arrFilter;
		}
		
		return $arrResult;
	}
	
	public function runSqlFunctions($clasName, $arrSql) {

		$tableName = $this->getTableName ($clasName);
		
		$functionRes = 0;
		if (count ( $arrSql ) > 0) {
			$arrKeys = array_keys ( $arrSql );
			foreach ( $arrKeys as $strKey ) {
				$strField = $arrSql ["$strKey"];
				$query = "SELECT $strKey($strField) FROM $tableName";
				$results_latest = $this->dbConnection->query ( $query );
				while ( $row = $results_latest->fetch_assoc () ) {
				   $functionRes=$row["$strKey($strField)"];
				}
			}
		}

		return $functionRes;

	}
	
//kamash

	public function dbexecute($sql) {

				$stmtDesc = $this->dbConnection->prepare ( $sql );
				$stmtDesc->execute ();
				$stmtDesc->close ();	
				return $stmtDesc;
	}	
	
	

}

?>